package cn.wipace.actionlib.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/5/5.
 */

public class DataUtil {

    /**
     * 时间戳转日期
     *
     * @param ms 传入秒
     * @return
     */
    public static Date transForDate(Integer ms) {
        if (ms == null) {
            ms = 0;
        }
        long msl = (long) ms * 1000;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date temp = null;
        if (ms != null) {
            try {
                String str = sdf.format(msl);
                temp = sdf.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return temp;
    }


    /**
     * 时间戳转日期
     *
     * @param ms
     * @return
     */
    public static Date transForDate3(Integer ms) {
        if (ms == null) {
            ms = 0;
        }
        long msl = (long) ms * 1000;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date temp = null;
        if (ms != null) {
            try {
                String str = sdf.format(msl);
                temp = sdf.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return temp;
    }

    /**
     * 时间戳转日期
     *
     * @param ms 传入毫秒
     * @return
     */
    public static Date transForDate(Long ms) {
        if (ms == null) {
            ms = (long) 0;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date temp = null;
        if (ms != null) {
            try {
                String str = sdf.format(ms);
                temp = sdf.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return temp;
    }


    public static String transForDate1(Long ms) {
        String str = "";
        if (ms != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (ms != null) {
                try {
                    str = sdf.format(ms);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }

    public static String transForDate2(Long ms) {
        String str = "";
        if (ms != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (ms != null) {
                try {
                    str = sdf.format(ms);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }

    public static String transForDate4(Long ms) {
        String str = "";
        if (ms != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
            if (ms != null) {
                try {
                    str = sdf.format(ms);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }


    public static String transForDate5(Long ms) {
        String str = "";
        if (ms != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            if (ms != null) {
                try {
                    str = sdf.format(ms);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }

    public static String transForDateInChinese(Long ms) {
        String str = "";
        if (ms != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

            if (ms != null) {
                try {
                    str = sdf.format(ms);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }

    /**
     * 日期转时间戳
     *
     * @param date
     * @return
     */
    public static Integer transForMilliSecond(Date date) {
        if (date == null) return null;
        return (int) (date.getTime() / 1000);
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static Integer currentTimeStamp() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    /**
     * 字符串转日期，格式为："yyyy-MM-dd HH:mm:ss"
     *
     * @param dateStr
     * @return
     */
    public static Date formatDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date result = null;
        try {
            result = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 字符串转日期，格式为："yyyy-MM-dd HH:mm:ss"
     *
     * @param dateStr
     * @return
     */
    public static Date formatDate(String dateStr, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date result = null;
        try {
            result = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = null;
        result = sdf.format(date);
        return result;
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String result = null;
        result = sdf.format(date);
        return result;
    }

    /**
     * 时间戳格式化输出（httl模版用）
     *
     * @param ms     时间戳
     * @param format 格式化
     * @return
     */
    public static String transForDate(Integer ms, String format) {
        String str = "";
        if (ms != null) {
            long msl = (long) ms * 1000;
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            if (!ms.equals(0)) {
                try {
                    str = sdf.format(msl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }

    /**
     * 取BigDecimal类型数的整数或小数部分（httl模版用）
     *
     * @param b    值
     * @param mode 模式 0取整 1去小数部分
     * @return
     */
    public static String splitBigDecimal(BigDecimal b, int mode) {
        DecimalFormat df = new DecimalFormat("0.00");
        String s = df.format(b);
        if (mode == 0) {
            return s.split("\\.")[0];
        } else {
            return "." + s.split("\\.")[1];
        }
    }


    /**
     * 获取某周的最后一天日期
     *
     * @param week
     * @return
     */
    public static String weekLastDay(int week) {
        Calendar c1 = Calendar.getInstance();
        int dow = c1.get(Calendar.DAY_OF_WEEK);
        c1.add(Calendar.DATE, -dow - 7 * (week - 1) + 1);
        String d1 = new SimpleDateFormat("yyyy-MM-dd").format(c1.getTime());
        return d1 + " 23:59:59";
    }


    /**
     * 时间戳转换为HH：mm：ss格式时间(日期去除)
     *
     * @author DingJiaCheng
     */
    public static String transToTime(int time) {
        String s = new String(transForDate1((long) time));
        String ss[] = s.split(" ");
        return ss[1];
    }

    public static int transToChuo(String dateString) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int res = 0;
        try {
            Date date = simpleDateFormat.parse(dateString);
            res = (int) date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 分数保留两位，格式化
     *
     * @return
     */
    public static String formatSocre(float score) {
        DecimalFormat df = new DecimalFormat("###,##0.00");
        String result = df.format(score);
        return result;
    }


}
